from rj import request
import time
from utils.file_operation import load_json_to_dict

rj = request.RequestHanlder()
for num in range(1, 287):
    print(f'开始上传第{num}页')
    json_data = load_json_to_dict(f'process/new_price_{num}.json')

    upload_list = []
    for item in json_data:
        upload_list.append({
            "productNum": item['productNum'],
            "price": item['price']
        })

        # 当 upload_list 长度为 50 时调用一次更新函数
        if len(upload_list) == 50:
            upload_res = rj.updateProductInfoByCode(upload_list).json()
            print(f'上传结果（第{num}页，分段）')
            print(upload_res)
            
            # 清空列表以准备下一组
            upload_list = []
            time.sleep(1)

    # 在循环外检查是否有剩余未上传的项目
    if upload_list:
        upload_res = rj.updateProductInfoByCode(upload_list).json()
        print(f'上传结果（第{num}页，剩余部分）')
        print(upload_res)
        time.sleep(1)

    time.sleep(1)
    
print('上传完成')

# logo_img = 'https://oss.caidehen.top/save/cxsw-logo.jpg'
# for num in range(1, 203):
#     print(f'开始上传第{num}页')
#     json_data = load_json_to_dict(f'data/Proteintech{num}.json')

#     temp_list = []

#     for item in json_data['data']:
#         # 如果长度小于五，则添加logo图片补足到5张
#         if len(item['photos']) < 5:
#             img_list = []
#             for _ in range(5 - len(item['photos'])):
#                 item['photos'].append({"photo": logo_img})

#             temp_list.append({
#                 "productNum": item['code'],
#                 "photos": item['photos']
#             })

#     upload_res = rj.updateProductInfoByCode(temp_list).json()
#     print(upload_res)
#     time.sleep(1)

# print('上传完成')

# 获取商品信息

# 判断商品图片是否满足5张

# 重新上传商品信息
